package com.yz.lzq.liwushuo.model;

import android.os.Handler;

import com.yz.lzq.liwushuo.bean.GiftBean;
import com.yz.lzq.liwushuo.interfaces.GiftInterface;
import com.yz.lzq.liwushuo.util.UrlConfig;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by AdminJia on 2016/11/24.
 */

public class GiftBiz implements IGiftBiz {
    private Handler handler = new Handler();

    @Override
    public void getMeiRiTuiJianTitleData(final OnGiftGetDataListener listener) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        GiftInterface meiRiTuiJianInterface = retrofit.create(GiftInterface.class);
        Call<GiftBean> call = meiRiTuiJianInterface.getGiftData();
        call.enqueue(new Callback<GiftBean>() {

            @Override
            public void onResponse(Call<GiftBean> call, Response<GiftBean>
                    response) {
                GiftBean body = response.body();
                List<GiftBean.DataBean.RanksBean> list = body.getData().getRanks();
                final String[] arrtitles = new String[list.size()];
                for (int i = 0; i < arrtitles.length; i++) {
                    arrtitles[i] = list.get(i).getName();


                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.getDataSuccess(arrtitles);
                    }
                });

            }

            @Override
            public void onFailure(Call<GiftBean> call, final Throwable t) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.getDataFaile(t);
                    }
                });
            }
        });

    }
}
